{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Caffe1 Models and Datasets\n",
    "\n",
    "This tutorial will help you acquire a variety of models and datasets and put them into places that the other tutorials will expect. We will primarily utilize Caffe's pre-trained models and the scripts that come in that repo. If you don't already have it, then clone it like so:\n",
    "\n",
    "```\n",
    "git clone https://github.com/BVLC/caffe.git\n",
    "```\n",
    "\n",
    "Start by importing the required modules."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "print(\"Required modules imported.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you can setup your root folder for Caffe below if you put it somewhere else. You should only be changing the path that's being set for CAFFE_ROOT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# You should have checked out original Caffe\n",
    "# git clone https://github.com/BVLC/caffe.git\n",
    "# change the CAFFE_ROOT directory below accordingly\n",
    "CAFFE_ROOT = os.path.expanduser('~/caffe')\n",
    "\n",
    "if not os.path.exists(CAFFE_ROOT):\n",
    "    print(\"Houston, you may have a problem.\") \n",
    "    print(\"Did you change CAFFE_ROOT to point to your local Caffe repo?\")\n",
    "    print(\"Try running: git clone https://github.com/BVLC/caffe.git\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's where you pick your model. There are several listed below such as AlexNet, GoogleNet, and Flickr Style. Uncomment the model you want to download."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Pick a model, and if you don't have it, it will be downloaded\n",
    "# format below is the model's folder, model's dataset inside that folder\n",
    "#MODEL = 'bvlc_alexnet', 'bvlc_alexnet.caffemodel' \n",
    "#MODEL = 'bvlc_googlenet', 'bvlc_googlenet.caffemodel'\n",
    "#MODEL = 'finetune_flickr_style', 'finetune_flickr_style.caffemodel'\n",
    "#MODEL = 'bvlc_reference_caffenet', 'bvlc_reference_caffenet.caffemodel'\n",
    "MODEL = 'bvlc_reference_rcnn_ilsvrc13', 'bvlc_reference_rcnn_ilsvrc13.caffemodel'\n",
    "\n",
    "# scripts to download the models reside here (~/caffe/models)\n",
    "# after downloading the data will exist with the script\n",
    "CAFFE_MODELS = os.path.join(CAFFE_ROOT, 'models')\n",
    "\n",
    "# this is like: ~/caffe/models/bvlc_alexnet/deploy.prototxt\n",
    "CAFFE_MODEL_FILE = os.path.join(CAFFE_MODELS, MODEL[0], 'deploy.prototxt')\n",
    "# this is like: ~/caffe/models/bvlc_alexnet/bvlc_alexnet.caffemodel\n",
    "CAFFE_PRETRAINED = os.path.join(CAFFE_MODELS, MODEL[0], MODEL[1])\n",
    "    \n",
    "# if the model folder doesn't have the goods, then download it\n",
    "# this is usually a pretty big file with the .caffemodel extension\n",
    "if not os.path.exists(CAFFE_PRETRAINED):\n",
    "    print(CAFFE_PRETRAINED + \" not found. Attempting download. Be patient...\")\n",
    "    os.system(\n",
    "        os.path.join(CAFFE_ROOT, 'scripts/download_model_binary.py') +\n",
    "        ' ' +\n",
    "        os.path.join(CAFFE_ROOT, 'models', MODEL[0]))\n",
    "else:\n",
    "    print(\"You already have \" + CAFFE_PRETRAINED)\n",
    "\n",
    "# if the .prototxt file was missing then you're in trouble; cannot continue\n",
    "if not os.path.exists(CAFFE_MODEL_FILE):\n",
    "    print(\"Caffe model file, \" + CAFFE_MODEL_FILE + \" was not found!\")\n",
    "else:\n",
    "    print(\"Now we can test the model!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
